<?php
/**
 * 前台 企业简介控制器
 * @author 17too
 * @link http://www.thankcms.com
 * @version 0.1
 */
class EnterpriseaboutAction extends Action
{
	/**
	 * 网站统一配置
	 *
	 * @var unknown_type
	 */
	public $Config;
	
	/**
	 * 构造函数
	 *
	 */
	public function _initialize()
	{
		$Config = D('Config');
		$this->Config = $Config->find('config_id=1');
	}
	/**
	 * 前台简介
	 */
	public function Show()
	{
		$Input = Input::getInstance();
		$Input->filter('addslashes');
		
		import('ORG.Util.HashMap');
		$conditions = new HashMap();
		$conditions->put('enter_id',array('EQ',$Input->get('enter_id')));
		//获取企业信息
		$Enterprise = D('Enterprise');
		$Enter = $Enterprise->where($conditions)->find();
		//获取企业简介
		$About = D('Enterpriseabout');
		$Content = $About->where($conditions)->find();
		
    	//组成模板 xxx@Index:Show
    	$UserTpl = $Enter['templates'] . '@Enterpriseabout:Show';
		
		//To templates
		$this->assign('Enterprise',$Enter);
		$this->assign('enter_id',$Input->get('enter_id'));
		$this->assign('About',$Content);
		$this->assign('Config',$this->Config);
		$this->display($UserTpl);
	}
    /**
     * 管理企业简介
     * 从数据库中查询是否已有相关企业的简介，
     * 如果有则跳入到修改简介页面，如果没有的跳转到创建简介页面
     */
    public function UserIndex()
    {
    	EasyACL('Enterpriseabout','UserIndex',$_SESSION);
    	$About = D('Enterpriseabout');
    	import('ORG.Util.HashMap');
    	$conditions = new HashMap();
    	$conditions->put('enter_id',array('EQ',$_SESSION['enter_id']));
    	$count = $About->count($conditions,'aid');
    	//判断并进行跳转
    	if ($count == 0) {
    		$this->redirect('UserCreate','Enterpriseabout');
    	} else {
    		$this->redirect('UserUpdate','Enterpriseabout');
    	}
    }
    /**
     * 创建新的简介
     */
    public function UserCreate()
    {
    	EasyACL('Enterpriseabout','UserCreate',$_SESSION);
    	//POST
    	if ($this->isPost()) {
    		$Input = Input::getInstance();
    		$Input->filter('addslashes');
    		
    		$About = D('Enterpriseabout');
    		$About->enter_id = $Input->post('enter_id');
    		$About->title = $Input->post('title');
    		$About->content = $Input->post('content');
    		if ($About->create()) {
    			if ($About->add()) {
    				$this->redirect('UserIndex','Enterpriseabout');
    			} else {
    				return $this->error('创建失败! 数据库操作失败');
    			}
    		} else {
    			return $this->error('尝试创建失败! 验证失败');
    		}
    	}
    	$this->assign('Config',$this->Config);
    	$this->display();
    }
    /**
     * 修改已有的简介信息
     */
    public function UserUpdate()
    {
    	EasyACL('Enterpriseabout','UserUpdate',$_SESSION);
    	$About = D('Enterpriseabout');
    	
    	$Input = Input::getInstance();
    	$Input->filter('addslashes');
    	
    	import('ORG.Util.HashMap');
    	$conditions = new HashMap();
    	
    	if ($this->isPost()) {
    		$conditions->put('enter_id',$_SESSION['enter_id']);
    		$About->where($conditions)->find();
    		$About->title = $Input->post('title');
    		$About->content = $Input->post('content');
    		if ($About->save()) {
    			halt('修改成功!');
    		} else {
    			return $this->error('尝试修改失败');
    		}
    	}
    	
    	//获取要修改的简介信息
    	$conditions->put('enter_id',$_SESSION['enter_id']);
    	$AboutContent = $About->where($conditions)->find();
    	
    	$this->assign('Config',$this->Config);
    	$this->assign('About',$AboutContent);
    	$this->display();
    }
    
}
?>